package com.example.api.service.impl;

import com.example.api.mapper.EmployeeMapper;
import com.example.api.model.entity.Employee;
import com.example.api.service.EmployeeService;
import com.example.api.utils.DataTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Employee save(Employee employee) {
        // 如果是新员工，设置ID
        if (employee.getId() == null || employee.getId().isEmpty()) {
            employee.setId(UUID.randomUUID().toString());
        }
        employee.setCreateAt(DataTimeUtil.getNowTimeString());
        employeeMapper.insert(employee);
        return employee;
    }

    @Override
    public void update(Employee employee) {
        employee.setUpdateAt(DataTimeUtil.getNowTimeString());
        employeeMapper.update(employee);
    }

    @Override
    public void delete(String id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public Employee findById(String id) {
        return employeeMapper.findById(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }
}
